from artifacts.base.artifacts_impl import *
from artifacts.base.base_artifacts import Artifacts
from artifacts.evaluate.general_score_evaluate import evaluate
from artifacts.base.artifacts_factory import ArtifactsFactory


score_need = 40


def complete(equip_map, backup_map):
    used_backup = False
    backup_type = None
    for artifacts_type in [Cup, Head, Clock, Flower, Plume]:
        if equip_map.get(artifacts_type) is None:
            if used_backup or backup_map.get(artifacts_type) is None:
                return False
            else:
                used_backup = True
                backup_type = artifacts_type
    if used_backup:
        equip_map[backup_type] = backup_map[backup_type]
    return True


def update_equip(equip_map, artifacts):
    artifacts.upgrade_max()
    score = evaluate(artifacts)
    if score >= score_need:
        cur_equip = equip_map.get(type(artifacts))
        if cur_equip is None or cur_equip['score'] < score:
            equip_map[type(artifacts)] = {'artifacts': artifacts, 'score': score}
            return True
    return False


def once_need_times(display=True):
    times = 0
    equip_map = {}
    backup_map = {}
    while True:
        times += 1
        artifacts_list = ArtifactsFactory.create_list()
        for artifacts in artifacts_list:
            artifacts.upgrade_max()
            score = evaluate(artifacts)
            if score >= score_need:
                if display:
                    for item in [{'artifacts': artifacts, 'score': score}]:
                        print(item['artifacts'])
                        print('score: %.1f\n' % item['score'])

                return times
